package com.baosight.sczx.common.zm.cInterface.ce;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;

/**
 * 
 * <p>Title: RerollingStatus.java</p>
 * <p>Description: 回滚设备中相关信息状态</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author wangkun
 * @version 1.0
 * <pre>Histroy:
 *          2012-2-24 下午03:07:56  wangkun  Create
 *</pre>
 */
public class RerollingStatus 
{
	private static final String _MAINTAIN_APPLICATION = "维修申请";
	private static final String _DEVICE_EXCEPTION = "异常";
	private static final String _WORK_PLAN = "工作计划";
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 
	 * <p>Function: Rerolling</p>
	 * <p>Description: 回滚方法</p>
	 * <p>inParam: 1.EiInfo 包含需要回滚内容的主键信息 </p>
	 * <p>inParam: 2.String block名称，如果填""，则默认为result</p>
	 * <p>inParam: 3.int 源数据类型 </p>
	 * <p>           1：工单</p>
	 * <p>           2：工作计划</p>
	 * <p>           3：异常记录</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午03:10:30 wangkun
	 * </p>
	 *
	 */
	public static void rerolling(EiInfo inInfo, String blockName, int resourceCode)
	{
		if(blockName == null || "".equals(blockName))
		{
			blockName = EiConstant.resultBlock;
		}
		
		switch(resourceCode)
		{
			case 1:
				for(int i=0; i<inInfo.getBlock(blockName).getRowCount(); i++)
				{
					String fdKeyId = inInfo.getBlock(blockName).getCellStr(i, "fdKeyId");
					String fdSource = inInfo.getBlock(blockName).getCellStr(i, "fdSource");
					String fdEquipmentId = inInfo.getBlock(blockName).getCellStr(i, "fdEquipmentId");
					if(_WORK_PLAN.equals(fdSource))
					{
						rerollingWorkPlan(fdKeyId, blockName, fdSource);
					}
					
					if(_DEVICE_EXCEPTION.equals(fdSource))
					{
						rerollingException(fdKeyId, blockName, fdEquipmentId, resourceCode, fdSource);
					}
					
					if(_MAINTAIN_APPLICATION.equals(fdSource))
					{
						rerollingApplication(fdKeyId, blockName, resourceCode, fdSource);
					}
					
					if(fdSource==null || "".equals(fdSource))
					{
						rerollingOnlyOneResource(fdEquipmentId, "在用", "完好");
					}
				}
				
				break;
			case 2: 
				for(int i=0; i<inInfo.getBlock(blockName).getRowCount(); i++)
				{
					String fdKeyId = inInfo.getBlock(blockName).getCellStr(i, "fdKeyId");
					String fdSource = inInfo.getBlock(blockName).getCellStr(i, "fdSource");
					String fdEquipmentId = inInfo.getBlock(blockName).getCellStr(i, "fdEquipmentId");
					
					if(_DEVICE_EXCEPTION.equals(fdSource))
					{
						rerollingException(fdKeyId, blockName, fdEquipmentId, resourceCode, fdSource);
					}
					
					if(_MAINTAIN_APPLICATION.equals(fdSource))
					{
						rerollingApplication(fdKeyId, blockName, resourceCode, fdSource);
					}
					
					if(fdSource==null || "".equals(fdSource))
					{
						rerollingOnlyOneResource(fdEquipmentId, "在用", "完好");
					}
				}
				
				break;
			case 3: 
					rerollingPhenomen(inInfo, blockName);
				break;
			default: 
				break;
		}
	}
	
	/**
	 * 
	 * <p>Function: rerollingOnlyWorkPlan</p>
	 * <p>Description: 回滚仅来源于工作计划</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午03:19:54 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void rerollingOnlyOneResource(String fdEquipmentId, String fdUsingType, String fdIntactType)
	{		
		Map inMap = new HashMap();
		inMap.put("fdKeyId", fdEquipmentId);
		inMap.put("fdUsingType", fdUsingType);
		inMap.put("fdIntactType", fdIntactType);
		inMap.put("fdDate", StaticTools.getDate());
		
		dao.update("ZMCEEquipmentCard.changeStatus", inMap);
	}
	
	/**
	 * 
	 * <p>Function: rerollingWorkPlan</p>
	 * <p>Description: 回滚工作计划</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午03:19:54 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void rerollingWorkPlan(String fdKeyId, String blockName, String fdSource)
	{		
		Map inMap = new HashMap();
		inMap.put("fdKeyId", fdKeyId);
		inMap.put("fdSource", fdSource);
		List _list = dao.query("ZMCEEquipmentOrder.queryInterface", inMap);
		
		if(_list.size() > 0)
		{
			String fdPlanId = ((Map)_list.get(0)).get("fdPlanId").toString();
			String fdEquipmentId = ((Map)_list.get(0)).get("fdEquipmentId").toString();
			inMap = new HashMap();
			inMap.put("fdPlanId", fdPlanId);
			dao.update("ZMCEWorkPlan.updateToPlanStatus", inMap);
			
			inMap.put("fdKeyId", fdPlanId);
			_list = dao.query("ZMCEWorkPlan.queryInterface", inMap);
			String fdType = ((Map)_list.get(0)).get("fdType").toString();
			
			inMap = new HashMap();
			if("大修".equals(fdType) || "维修".equals(fdType))
			{
				rerollingOnlyOneResource(fdEquipmentId, "待修", "不完好");
			}
		}
	}
	
	/**
	 * 
	 * <p>Function: rerollingException</p>
	 * <p>Description: 回滚工单或工作计划对应的异常信息</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午04:34:47 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void rerollingException(String fdKeyId, String blockName, String fdEquipmentId, int resourceCode, String fdSource)
	{
		Map inMap = new HashMap();
		String sql = "ZMCEEquipmentOrder.queryInterface";
		if(resourceCode == 1)
		{
			sql = "ZMCEEquipmentOrder.queryInterface";
		}
		else if(resourceCode == 2)
		{
			sql = "ZMCEWorkPlan.queryInterface";
		}
		else
		{
			return;
		}
		
		inMap.put("fdKeyId", fdKeyId);
		inMap.put("fdSource", fdSource);
		List _list = dao.query(sql, inMap);
		
		if(_list.size() > 0)
		{
			String keyId = "";
			if(resourceCode == 1)
			{
				keyId = ((Map)_list.get(0)).get("fdPlanId").toString();
			}
			else
			{
				keyId = ((Map)_list.get(0)).get("fdSourceId").toString();
			}
			inMap = new HashMap();
			inMap.put("fdKeyId", keyId);
			inMap.put("fdStatus", "未处理");
			dao.update("ZMCEUnusualProblem.updateStatus", inMap);
			_list = dao.query("ZMCEUnusualProblem.queryInterface", inMap);
			
			if(_list.size() > 0)
			{
				Object fdWorkRecordId = ((Map)_list.get(0)).get("fdWorkRecordId");
				
				inMap = new HashMap();
				inMap.put("fdKeyId", fdWorkRecordId);
				inMap.put("fdStatus", "未处理");
				dao.update("ZMCEFaultRecord.updateStatus", inMap);
			}
			
			rerollingOnlyOneResource(fdEquipmentId, "在用", "完好");
		}
	}
	
	/**
	 * 
	 * <p>Function: rerollingApplication</p>
	 * <p>Description: 回滚工单或工作计划对应的维修申请</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午04:34:47 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void rerollingApplication(String fdKeyId, String blockName, int resourceCode, String fdSource)
	{
		Map inMap = new HashMap();
		String sql = "ZMCEEquipmentOrder.queryInterface";
		if(resourceCode == 1)
		{
			sql = "ZMCEEquipmentOrder.queryInterface";
		}
		else if(resourceCode == 2)
		{
			sql = "ZMCEWorkPlan.queryInterface";
		}
		else
		{
			return;
		}
		
		inMap.put("fdKeyId", fdKeyId);
		inMap.put("fdSource", fdSource);
		List _list = dao.query(sql, inMap);
		
		if(_list.size() > 0)
		{
			String keyId = "";
			String fdEquipmentId = ((Map)_list.get(0)).get("fdEquipmentId").toString();
			if(resourceCode == 1)
			{
				keyId = ((Map)_list.get(0)).get("fdPlanId").toString();
			}
			else
			{
				keyId = ((Map)_list.get(0)).get("fdSourceId").toString();
			}
			
			inMap = new HashMap();
			inMap.put("keyID", keyId);
			inMap.put("fdStatus", "待处理");
			dao.update("ZMCEMaintenanceApply.updateStatus", inMap);
			
			rerollingOnlyOneResource(fdEquipmentId, "在用", "完好");
		}
	}
	
	/**
	 * 
	 * <p>Function: rerollingPhenomen</p>
	 * <p>Description: 回滚异常对应的故障</p>
	 * <p>return value:void</p>
	 * <p>History:
	 *    2012-2-24下午04:48:31 wangkun
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static void rerollingPhenomen(EiInfo inInfo, String blockName)
	{
		for(int i=0; i<inInfo.getBlock(blockName).getRowCount(); i++)
		{
			String fdKeyId = inInfo.getBlock(blockName).getCellStr(i, "fdKeyId");
			Map inMap = new HashMap();
			inMap.put("fdKeyId", fdKeyId);
			List _list = dao.query("ZMCEUnusualProblem.queryInterface", inMap);
				
			if(_list.size() > 0)
			{
				String fdWorkRecordId = ((Map)_list.get(0)).get("fdWorkRecordId").toString();
				
				inMap = new HashMap();
				inMap.put("fdKeyId", fdWorkRecordId);
				inMap.put("fdStatus", "已处理");
				dao.update("ZMCEFaultRecord.updateStatus", inMap);
			}
		}
	}
}
